import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';

import '../../themes/appTheme.dart';

class DeleteFolderDialog extends StatefulWidget {
  DeleteFolderDialog({
    Key? key,
    required this.title,
    required this.message,
  }) : super(key: key);
  final String title;
  final String message;

  @override
  State<DeleteFolderDialog> createState() => _DeleteFolderDialogState();
}

class _DeleteFolderDialogState extends State<DeleteFolderDialog> {
  final BooksController _bookscontroller = Get.find();
  String? folderId;
  String? folderName;

  @override
  void initState() {
    folderId = _bookscontroller.getCurrentBookFolder().id;
    folderName = _bookscontroller.getCurrentBookFolder().folderName;
    super.initState();
  }

  @override
  void dispose() {
    //_bookscontroller.fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
    super.dispose();
  }

  void _deleteFolder() async {
    Log.debug('deleted folder id$folderId');
    _bookscontroller.deleteBookLibraryFolder(folderId!).then((data) {
      if (data.message != null && data.status == 1000) {
        _bookscontroller.fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
        showSnackMsg(data.message.toString());
        // Get.snackbar('Message'.tr, data.message.toString(),
        //     backgroundColor: Theme.of(context).primaryColor,
        //     colorText: Theme.of(context).primaryColorLight,
        //     snackPosition: SnackPosition.BOTTOM);
        Navigator.of(context).pop();
      } else {
        showSnackMsg(data.message.toString());
        // Get.snackbar('Message'.tr, data.message.toString(),
        //     backgroundColor: Theme.of(context).primaryColor,
        //     colorText: Theme.of(context).primaryColorLight,
        //     snackPosition: SnackPosition.BOTTOM);
      }
    });
    // Future.delayed(Duration(milliseconds: 50), () {
    //   _bookscontroller.fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
    // });
    // //_bookscontroller.fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(this.widget.title),
      titleTextStyle: TextStyle(
          letterSpacing: 1,
          color: Get.isDarkMode ? Colors.white : Colors.black,
          fontSize: 20),
      backgroundColor:
          Get.isDarkMode ? AppTheme.colors.darkTheme2 : Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(6.0),
        ),
      ),
      content: Container(
        height: 200,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Padding(
                  padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                  child: Text.rich(TextSpan(
                      text: this.widget.message + ' ',
                      children: <InlineSpan>[
                        TextSpan(
                          text: 'DELETE',
                          style: TextStyle(
                              color: Get.isDarkMode
                                  ? Colors.white
                                  : AppTheme.colors.pinkBtn,
                              fontSize: 18),
                        ),
                        TextSpan(
                          text: ' folder ',
                        ),
                        TextSpan(
                          text: folderName,
                          style: TextStyle(color: Colors.blue, fontSize: 18),
                        ),
                        TextSpan(
                          text: '?',
                        )
                      ])),
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                MaterialButton(
                    color:
                        Get.isDarkMode ? (Color(0xff444444)) : (Colors.white),
                    textColor: Get.isDarkMode
                        ? (Color(0xffcccccc))
                        : Theme.of(context).primaryColorDark,
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: Text('Cancel'.tr)),
                MaterialButton(
                    color: Get.isDarkMode
                        ? (Colors.white)
                        : Theme.of(context).primaryColorDark,
                    textColor: Get.isDarkMode
                        ? (Colors.black)
                        : Theme.of(context).primaryColorLight,
                    onPressed: () {
                      _deleteFolder();
                    },
                    child: Text('Submit'.tr))
              ],
            ),
          ],
        ),
      ),
    );
  }
}
